#coding:utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

import requests
import confs
import time

class Tools(object):
	"""docstring for Tools"""
	def __init__(self, AppKey, url, dtype='json'):
		super(Tools, self).__init__()
		self.AppKey = AppKey
		self.url = url
		self.dtype = dtype

	def webscan(self, domain):
		payload = {'domain' : domain}
		return self._request(payload=payload)

	def ip(self, ip):
		payload = {'ip' : ip}
		return self._request(payload=payload)

	def question(self, info):
		payload = {'info' : info}
		return self._request(payload=payload)

	def small(self, sort='desc', page=1, pagesize=1):
		payload = {
			'sort' : sort,
			'page' : page,
			'pagesize' : pagesize,
			'time' : int(time.time())
		}
		return self._request(payload=payload)

	def phone(self, phone):
		payload = { 'phone' : phone }
		return self._request(payload=payload)

	def _request(self, payload={}, headers={}):
		try:
			payload['key'] = self.AppKey
			payload['dtype'] = self.dtype
			print payload
			r = requests.get(self.url, params=payload, headers=headers)
			return r.json()
		except Exception as e:
			raise e

if __name__ == '__main__':
	url, AppKey = confs.juhe_api_key['phone']
	t = Tools(AppKey=AppKey, url=url)
	data = t.phone(phone='15010220814')['result']
	print data
	print data['province'],data['city']


		